<template>
  <div class="main">
    <!-- 轮播图 -->
    <a-carousel class="swiper">
      <div class="slide">
        <router-link to="/bookinfo/https%3A%2F%2Fwww.81book.com%2Fbook%2F10978%2F">
        <img src="https://s0.ifengimg.com/2019/08/28/2_405074da.jpg" alt />
        </router-link>
      </div>
      <div class="slide">
        <router-link to="/bookinfo/https%3A%2F%2Fwww.81book.com%2Fbook%2F25096%2F">
        <img src="https://s0.ifengimg.com/2019/08/27/1_7f3167bb.jpg" alt />
        </router-link>
      </div>
      <div class="slide">
        <router-link to="/bookinfo/https%3A%2F%2Fwww.81book.com%2Fbook%2F7683%2F">
        <img src="https://s0.ifengimg.com/2019/08/28/11_36e5b03f.png" alt />
        </router-link>
      </div>
    </a-carousel>

    <!-- 今日推荐 -->
    <div class="today">
      <h3>今日推荐</h3>
      <span class="refresh" @click="getRecommendList">
        <img src="../assets/refresh.png" alt="刷新"  /> 换一批
      </span>
    </div>
    <ul class="recommend">
      <li v-for="item in recommendBooks" :key="item.bookName+1" @click="goBookInfo(item)">
        <div class="pic"><img :src="item.picUrl" alt /></div>
        <div class="bookName">《{{item.bookName}}》</div>
        <div class="author">{{item.author}}</div>
        <div class="intro">{{item.intro}}</div>
      </li>
    </ul>


    <!-- 排行榜 -->
    <div class="paihang">
     <h3>排行榜</h3>
    </div>
    <ul class="rank">
      <li v-for="item in rankBooks" :key="item.bookName+2" @click="goBookInfo(item)">
        <div class="pic"> <img :src="item.picUrl" alt="" /></div>
        <div class="bookName">《{{item.bookName}}》</div>
        <div class="author">{{item.author}}</div>
      </li>
      <li></li>
      <li></li>
    </ul>
    <div style="height:60px"></div>
    <Footer></Footer>
  </div>
</template>

<script>
import Footer from "../components/Footer.vue";
import {getRecommend,getRank} from '@/api/index.js'
import {Carousel} from 'ant-design-vue'

export default {
  data: () => {
    return {
      recommendBooks: [],
      rankBooks:[],
      swiperOption: {
        autoplay: {
          delay: 5000
        },
        loop: true,
        speed: 1000,
        effect:'fade',
        pagination: {
          el: ".swiper-pagination"
        }
      }
    };
  },
  components: {
    Footer,
    'a-carousel':Carousel
  },
  created() {
    this.getRecommendList();
    this.getRankList();
  },
  methods: {
    // 获取推荐列表
    async getRecommendList() {
      try {
        let res = await getRecommend()
        this.recommendBooks = res.data;
      } catch (error) {
        console.log(error)
      }
    },

    // 获取排行列表
    getRankList(){
      getRank().then(res => {
        this.rankBooks = res.data;
      });
    },

    // 跳转到小说详情
    goBookInfo(book){
      var bookUrl =encodeURIComponent(book.url)
      this.$router.push({path:'/bookinfo/'+bookUrl})
    }
  }
};
</script>

<style lang='less' scoped>
@media screen and(max-width:420px) {
  .main {
    width: 100%;
    font-family: "黑体";
  }
  .swiper {
    width: 96%;
    margin: 0 auto;
    .slide {
      width: 100%;
      img {
        width: 100%;
        border-radius: 5px;
      }
    }
    .swiper-pagination-bullet-active {
      background: white;
    }
  }
  h3 {
    text-align: center;
    font-weight: normal;
    letter-spacing: 5px;
    margin-bottom: 0;
  }
  .today {
    position: relative;
    width: 100%;
    height: 40px;
    margin-top: 5px;
    h3 {
      position: absolute;
      width: 100%;
      line-height: 40px;
    }
    .refresh {
      position: absolute;
      right: 0;
      bottom: 10px;
      width: 80px;
      height: 20px;
      display: flex;
      align-items: center;
      font-size: 12px;
      color: gray;
      img {
        width: 20px;
        height: 20px;
      }
    }
  }
  .recommend {
    display: flex;
    justify-content: space-around;
    flex-wrap: wrap;
    margin-top: -15px;
    li {
      width: 100px;
      height: 190px;
      margin-top: 15px;
      .pic{
        width: 100px;
        height: 140px;
        background: url(../assets/cover.jpg);
        background-size: 100% 100%
      }
      img {
        width: 100%;
        height: 100%;
      }
      .bookName {
        width: 100%;
        font-weight: 300;
        font-size: 14px;
        margin-top: 5px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
      .author {
        font-weight: 300;
        font-size: 14px;
        color: gray;
        // margin-top: 5px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
      .intro{
        display: none
      }
    }
    li:nth-child(4),li:nth-child(5){
        display: none
    }
  }
  .paihang{
    margin-top: 20px;
  }
  .rank{
    display: flex;
    justify-content: space-around;
    flex-wrap: wrap;
    li {
      width: 100px;
      height: 190px;
      margin-top: 15px;
      img {
        width: 100%;
        height: 140px;
      }
      .bookName {
        width: 100%;
        font-weight: 300;
        font-size: 14px;
        margin-top: 5px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
      .author {
        font-weight: 300;
        font-size: 14px;
        color: gray;
        // margin-top: 5px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
      .intro{
        display: none
      }
    }
  }
}
@media screen and(min-width:420px) {
  .main {
    width: 100%;
  }
  .swiper{
    img{
      width: 100%
    }
    .swiper-pagination-bullet-active {
      background: white;
    }
  }
  .today{
    width: 1100px;
    height: 80px;
    background: url(../assets/tit-bg.png) top center no-repeat;
    background-size: contain;
    position: relative;
    h3{
      position: absolute;
      left: 50px;
      top: 5px;
      line-height: 60px;
      font-size: 20px;
      color: white;
    }
    .refresh{
      position: absolute;
      left: 200px;
      display: flex;
      align-items: center;
      top: 25px;
      color: lightgray;
      cursor: pointer;
      img{
        width: 20px;
        height: 20px;
      }
      
    }
  }
  .recommend{
    position: relative;
    height: 500px;
    li{
      position: absolute;
      overflow: hidden;
    }
    
    li:nth-child(1){
      width: 300px;
      height: 480px;
      left: 20px;
      .pic{
        width: 300px;
        height: 400px;
        background: url(../assets/cover.jpg);
        background-size: 100% 100%
      }
      img{
        width: 300px;
        height: 400px;
      }
      .bookName{
        color: rgb(55, 143, 226);
        text-align: center
      }
      .author{
        display: none
      }
      .intro{
        margin-top: 5px;
        font-size: 14px;
        overflow: hidden;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp:2;
        text-indent: 2em
      }
    }
    li:nth-child(2),li:nth-child(3),li:nth-child(4),li:nth-child(5){
      width: 350px;
      height: 203px;
      border-bottom: 1px solid #d3d3d3;
      .pic{
        width: 150px;
        height: 200px;
        background: url(../assets/cover.jpg);
        background-size: 100% 100%
      }
      img{
         width: 150px;
         height: 200px;
         position: absolute
      }
      .bookName{
        position: absolute;
        top: 15px;
        left: 160px
      }
      .author{
        position: absolute;
        top: 50px;
        left: 165px;
        font-size: 14px
      }
      .author:before{
        content: '作者：'
      }
      .intro{
        position: absolute;
        top: 80px;
        left: 160px;
        overflow: hidden;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp:4;
        text-indent: 2em;
        font-size: 14px
      }
      
    }
     li:nth-child(2){
       left: 360px;
       top: 15px
     }
      li:nth-child(3){
       right: 20px;
       top: 15px
     }
     li:nth-child(4){
       left: 360px;
       top: 240px
     }
     li:nth-child(5){
        right: 20px;
        top: 240px
     }
     

     
  }
  .paihang{
    
    width: 1100px;
    height: 80px;
    background: url(../assets/tit-bg.png) top center no-repeat;
    background-size: contain;
    position: relative;
    h3{
      position: absolute;
      top: 5px;
      left: 50px;
      line-height: 60px;
      font-size: 20px;
      color: white;
    }
  }
  .rank{
    width: 1100px;
    display: flex;
    justify-content: space-around;
    flex-wrap: wrap;
    li{
      width:150px;
      height: 250px;
      margin: 20px;
      .pic{
        width: 150px;
        height: 210px;
        background: url(../assets/cover.jpg);
        background-size: 100% 100%
      }
      img{
        width: 150px;
        height: 210px;
      }
      .bookName{
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
      .author:before{
        content: '作者：'
      }
      .author{
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        font-size: 14px
      }
    }
  }
}
</style>